package org.phoenixframework.api.search.controller;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.phoenixframework.api.search.service.IPmsProductService;
import org.phoenixframework.common.entity.pms.PmsProduct;
import org.phoenixframework.common.tools.ResultModel;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * pms-商品信息 前端控制器
 * </p>
 *
 * @author doublelife
 * @since 2021-07-18
 */
@Slf4j
@Api(tags = {"ES商品信息搜索"})
@AllArgsConstructor
@RestController
@RequestMapping("/pmsProduct")
public class PmsProductController extends BaseController {

    private final IPmsProductService pmsProductService;

    @ApiOperation(value = "从数据库中导入所有商品到ES")
    @PostMapping("/importAll")
    public ResultModel<?> importAll() {
        pmsProductService.importAll();
        return ResultModel.ok();
    }

    @ApiOperation(value = "简单搜索")
    @GetMapping("/search/simple/{current}/{size}")
    public ResultModel<?> search(@RequestParam(required = false) String keyword,
                                 @ApiParam(name = "current", value = "当前页码", required = true)
                                 @PathVariable int current,
                                 @ApiParam(name = "size", value = "每页记录数", required = true)
                                 @PathVariable int size) {
        PageRequest page = PageRequest.of(current, size);
        Page<PmsProduct> pmsProducts = pmsProductService.search(keyword, page);
        return buildByObject(pmsProducts);
    }
}
